package com.promis.party.helper;

import android.content.Context;
import android.content.Intent;

import com.promis.okhttp.HttpJsonParams;
import com.promis.party.preference.AccountPreferences;
import com.promis.party.preference.UserPreferences;
import com.promis.party.ui.LoginActivity;

/**
 * Created by zcy on 2018/9/25.
 */

public class LoginHelper {

    public static void doLogin(ContextHelper contextHelper, final UserLoginListener listener) throws Exception {
        AccountPreferences preferences = AccountPreferences.getInstance(contextHelper.getContext());
        if (preferences.exists()) {
            String account = preferences.getAccount();
            String password = preferences.getPassword();

            HttpJsonParams params = new HttpJsonParams();
//            params.add("account", account);
//
//            PBECodeCipher cipher = PBECodeCipher.getInstance();
//            byte[] md5Key = MD5Digest.toKey(password);
//
//            byte[] salt = cipher.initSalt();
//            SecretKey secretKey = cipher.toKey(account);
//
//            byte[] data = cipher.encrypt(md5Key, secretKey, salt);
//
//            String pwd = cipher.encryptBASE64(data);
//            String ransalt = cipher.encryptBASE64(salt);
//            params.add("password", pwd);
//            params.add("ransalt", ransalt);
////            params.add("cid", PushManager.getInstance().getClientid(contextHelper.getContext()));
//
//            UserPreferences.getInstance(contextHelper.getContext()).remove();
//            AccountPreferences.getInstance(contextHelper.getContext()).remove();
//            String url = new ContextHelper(contextHelper.getContext()).getServiceUrl(R.string.login);
//            new HttpClient<JsonLogin>(contextHelper.getContext()) {
//                @Override
//                protected void onSuccess(JsonLogin result) {
//                    User user = result.getUser();
//                    if (user != null) {
//                        UserPreferences.getInstance(getContext()).put(user);
//                        AccountPreferences.getInstance(getContext()).put(user.getAccount(), user.getPassword(), user.getState());
//                    }
////                    PushManager.getInstance().bindAlias(getContext(), user.getId(), user.getId());
//
////                    Tag[] tagParam = null;
////                    //标签
////                    if(AuthType.PERSONSTORE.getValue() == user.getAuthType()) {
////                        String[] tags = new String[] {PushType.PERSONSTORE.getExplain()};
////                        tagParam = new Tag[tags.length];
////
////                        for (int i = 0; i < tags.length; i++) {
////                            Tag t = new Tag();
////                            //name 字段只支持：中文、英文字母（大小写）、数字、除英文逗号和空格以外的其他特殊符号, 具体请看代码示例
////                            t.setName(tags[i]);
////                            tagParam[i] = t;
////                        }
////                    }else if(AuthType.LICENSE.getValue() == user.getAuthType()) {
////                        String[] tags = new String[] {PushType.STORE.getExplain()};
////                        tagParam = new Tag[tags.length];
////
////                        for (int i = 0; i < tags.length; i++) {
////                            Tag t = new Tag();
////                            //name 字段只支持：中文、英文字母（大小写）、数字、除英文逗号和空格以外的其他特殊符号, 具体请看代码示例
////                            t.setName(tags[i]);
////                            tagParam[i] = t;
////                        }
////                    }else if(AuthType.PUBLICWELFARE.getValue() == user.getAuthType()) {
////                        String[] tags = new String[] {PushType.ORGANIZE.getExplain()};
////                        tagParam = new Tag[tags.length];
////
////                        for (int i = 0; i < tags.length; i++) {
////                            Tag t = new Tag();
////                            //name 字段只支持：中文、英文字母（大小写）、数字、除英文逗号和空格以外的其他特殊符号, 具体请看代码示例
////                            t.setName(tags[i]);
////                            tagParam[i] = t;
////                        }
////                    }
////                    PushManager.getInstance().setTag(getContext(),tagParam,System.currentTimeMillis() +"");
//
//                    if (listener != null) listener.onSuccess();
//                }
//
//                @Override
//                protected void onFinish() {
//                    if (listener != null) listener.onFinish();
//                }
//            }.postAsyn(url, params);
        }
    }

    public static class UserLoginListener {
        public void onSuccess() {}
        public void onFinish() {}
    }

    public static boolean isLogined(Context context) {
        if (!UserPreferences.getInstance(context).exists()) {
            Intent intent = new Intent(context, LoginActivity.class);
            //如果使用Context的startActivity方法的話，就需要开启一个新的的task，
            // 遇到这个异常，是因为使用了Context的startActivity方法。解决办法是，加一个flag。
            intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK );
            context.startActivity(intent);
            return false;
        } else return true;
    }
}
